import com.sun.jersey.api.client.*;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.client.urlconnection.HTTPSProperties;

import javax.net.ssl.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.*;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

/**
 * Created by andrew.li on 11/20/15.
 */

//@Component
public class ClientSingletonSsl implements ClientHandler {

//    private Client client;
    private SSLContext sslContext;

//    @Value("${client.cer.certifacatePath}")
    private String certifacatePath="C:\\yuhuaming\\keystore\\tserver.keystore";
//    @Value("${client.cer.password}")
    private String password;
//    @Value("${client.cer.tlsVer}")
    private String tlsVer;
//    private  int timeout;


    public ClientSingletonSsl(String certifacatePath, String password, String tlsVer) {
        this.certifacatePath = certifacatePath;
        this.password = password;
        this.tlsVer = tlsVer;
    }

    //    @PostConstruct
    public void ClientSingletonSslInit() throws IOException, CertificateException, NoSuchAlgorithmException, UnrecoverableKeyException, KeyStoreException, KeyManagementException {
        ClientConfig config = new DefaultClientConfig();
        SSLContext ctx = null;

        TrustManager[] myTrustManager = new TrustManager[]{
                new X509TrustManager() {
                    public X509Certificate[] getAcceptedIssuers() {
                        return null;
                    }
                    public void checkClientTrusted(X509Certificate[] certs, String authType) {

                    }
                    public void checkServerTrusted(X509Certificate[] certs, String authType) {
                        //                            throw new RuntimeException();
                    }
                }
        };
//        ctx = SSLContext.getInstance("TLS");"TLSv1.2"
        ctx = SSLContext.getInstance(tlsVer);

            KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
            TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");

            KeyStore ks = KeyStore.getInstance("PKCS12");
            KeyStore tks = KeyStore.getInstance("PKCS12");

            ks.load(new FileInputStream(certifacatePath), password.toCharArray());
            tks.load(new FileInputStream(certifacatePath), password.toCharArray());
            tmf.init(tks);
            kmf.init(ks, password.toCharArray());
            KeyManager[] km = kmf.getKeyManagers();
            TrustManager[] trustManagers=tmf.getTrustManagers();
            ctx.init(km, myTrustManager, null);
//            ctx.init(km, trustManagers, null);
//        }else{
//            ctx.init(null,myTrustManager,new SecureRandom());
//        }

        config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(new HostnameVerifier() {
            public boolean verify(String s, SSLSession sslSession) {
                return true;
            }
        }, ctx));
        sslContext=ctx;
       /* client = Client.create(config);
        if(timeout>0){
            client.setReadTimeout(timeout);
            client.setConnectTimeout(timeout);
        }else{
            client.setReadTimeout(6*1000);
            client.setConnectTimeout(6*1000);
        }*/

    }
/*    public Client getClient(){
        return client;
    }*/

    public SSLContext getSslContext(){
        return sslContext;
    }
    @Override
    public ClientResponse handle(ClientRequest clientRequest) throws ClientHandlerException {
        return null;
    }

    public String getCertifacatePath() {
        return certifacatePath;
    }

    public void setCertifacatePath(String certifacatePath) {
        this.certifacatePath = certifacatePath;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getTlsVer() {
        return tlsVer;
    }

    public void setTlsVer(String tlsVer) {
        this.tlsVer = tlsVer;
    }


}
